# -*- coding: utf-8 -*-

from __future__ import print_function

import jieba.analyse
import matplotlib.pyplot as plt
from wordcloud import WordCloud

# 设置相关的文件路径
bg_image_path = "E:/Projects/PyCharmProjects/Common/wordcloudDemo/core/pic/image1.jpg"
text_path = 'E:/Projects/PyCharmProjects/Common/wordcloudDemo/jsjs.txt'
font_path = 'E:/Projects/PyCharmProjects/Common/wordcloudDemo/core/msyh.ttf'
stopwords_path = 'E:/Projects/PyCharmProjects/Common/wordcloudDemo/core/stopword.txt'


class dealWords:
    def __init__(self, content):
        self.content = content

    def clean_using_stopword(self, text):
        """
        去除停顿词，利用常见停顿词表+自建词库
        :param text:
        :return:
        """
        mywordlist = []
        seg_list = jieba.cut(text, cut_all=False)
        liststr = "/".join(seg_list)
        with open(stopwords_path, 'rb') as f_stop:
            f_stop_text = f_stop.read()
            f_stop_text = unicode(f_stop_text, 'utf-8')
        f_stop_seg_list = f_stop_text.split('\n')
        for myword in liststr.split('/'):  # 去除停顿词，生成新文档
            if not (myword.strip() in f_stop_seg_list) and len(myword.strip()) > 1:
                mywordlist.append(myword)
        return ''.join(mywordlist)

    def preprocessing(self):
        """
        文本预处理
        :return:
        """
        # with open(text_path, 'rb') as f:
        #     content = f.read()
        return self.clean_using_stopword(self.content)
        # return content

    def extract_keywords(self):
        """
        利用jieba来进行中文分词。
        analyse.extract_tags采用TF-IDF算法进行关键词的提取。
        :return:
        """
        # 抽取1000个关键词，带权重，后面需要根据权重来生成词云
        allow_pos = ('nr',)  # 词性
        tags = jieba.analyse.extract_tags(self.preprocessing(), 100, withWeight=True)
        keywords = dict()
        count = 1
        for i in tags:
            print("%d---%s---%f" % (count, i[0], i[1]))
            count += 1
            keywords[i[0]] = i[1]
        return keywords

    def draw_wordcloud(self):
        """
        生成词云。1.配置WordCloud。2.plt进行显示
        :return:
        """
        back_coloring = plt.imread(bg_image_path)  # 设置背景图片
        # 设置词云属性
        wc = WordCloud(font_path=font_path,  # 设置字体
                       background_color="white",  # 背景颜色
                       max_words=50,  # 词云显示的最大词数
                       # mask=back_coloring,  # 设置背景图片
                       )

        # 根据频率生成词云
        wc.generate_from_frequencies(self.extract_keywords())
        # 显示图片
        # plt.figure()
        # plt.imshow(wc)
        # plt.axis("off")
        # plt.show()
        # 保存到本地
        wc.to_file("wordcloud.jpg")
